define(function(require, exports, module) {
	var RemoteCache=require('resources/js/component/RemoteCache.js');
	
    var ZTreeMenu=function($div,config){
    	this.$div=$div;
    	
    	this.menuId=config.menuId;
    	this.curItem=config.curItem;
    	if(config.data==undefined)
    		this.dataUrl=config.dataUrl;
    	else
    		this.data=config.data;
    	this.onNodeClick=config.onNodeClick;
        this.init();
    };
    
    ZTreeMenu.remoteCache=new RemoteCache();
    
    ZTreeMenu.prototype={
        init:function(){
            this.render();
            this.bind();
            this.sync();
        },
        curMenu:{},
        render:function(){
        	var that=this,
        		setting = {
        			view: {
        				showLine: false,
        				showIcon: false,
        				selectedMulti: false,
        				dblClickExpand: false,
        				addDiyDom: function(treeId, treeNode){
        					var spaceWidth =16;
        					var switchObj = $("#" + treeNode.tId + "_switch"),
        					icoObj = $("#" + treeNode.tId + "_ico");
        					switchObj.remove();
        					icoObj.before(switchObj);

        					if (treeNode.level > 0) {
        						var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level)+ "px'></span>";
        						switchObj.before(spaceStr);
        					}
        				}
        			},
        			data: {
        				simpleData: {
        					enable: true
        				}
        			},
        			callback: {
        				beforeClick: function(treeId, treeNode) {
        					var curMenu=that.curMenu,zTree_Menu=that.ztreeObj;
        					if (treeNode.level == 0 ) 
        					{
        						that.ztreeObj.expandNode(treeNode);
        						$.each(zTree_Menu.getNodes(),function(index,node){
        							if(node!=treeNode)
        								zTree_Menu.expandNode(node,false);
        						})
        						return false;
        					}
        					
        					var nodes=that.ztreeObj.getSelectedNodes();
        					if(nodes.length>0&&nodes[0]==treeNode)
        		        	{
        		        		that.ztreeObj.cancelSelectedNode(treeNode)
        		        		that.onNodeClick&&that.onNodeClick(treeId, treeNode);
        		        		return false;
        		        	}
        					
        					return true;
        				},
        				onClick:function(event, treeId, treeNode) {
        					if(treeNode.level == 0)
        						return false;
        					that.onNodeClick&&that.onNodeClick(treeId, treeNode);
        					return true;
        				}
        			}
        		};
        	if(this.data==undefined)
        		this.data=ZTreeMenu.remoteCache.get(this.menuId,this.dataUrl);
			this.ztreeObj= $.fn.zTree.init(this.$div, setting, this.data);
        },
        bind:function(){
        	var $div=this.$div;
        	$div.hover(function () {
				if (!$div.hasClass("showIcon")) {
					$div.addClass("showIcon");
				}
			}, function() {
				$div.removeClass("showIcon");
			});
        },
        sync:function(){
        	this.curItem&&this.ztreeObj.selectNode(this.ztreeObj.getNodeByParam('id',this.curItem));
        },
        getCurItemId:function(){
        	var nodes=this.ztreeObj.getSelectedNodes();
        	if(nodes.length>0)
        		return nodes[0].id;
        }
    };
    
    return ZTreeMenu;
}); 